<?php
namespace Home\Controller;
use Think\Controller;
class GameController extends BaseController {
    // public function gamelist(){
    //     $games=M('games');#调用表
    //     $this->gamelist=$games->select();##调动成员方法
    //     dump($this->gamelist);
    //     $this->display();

    // $gamesmodel=M('games');
        // $games=$gamesmodel->order('game_id desc')->select();
        // $this->games=$games;
        // $this->display();
    // }
    // 崔晓真 比赛列表
    public function gamelist(){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;

        $games = M('games');
        $qrcodenum=$games->count();
        $Page = new \Think\Page($qrcodenum,4);
        $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
    每页<b>4</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme',' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->pageNav=$Page->show();//在页面{pageNav}
        //$this->link=$Page->show();
        $this->qrcodeList=$games->limit($Page->firstRow,4)->order('game_id desc')->select();
        $this->display();
	}

    // 崔晓真 删除比赛
    public function delete($game_id){
        $gamesModel=M('games');
        $res=$gamesModel->where("game_id=$game_id")->find();
        $photoPath = realpath("./Public/upload/gamepic/".$res['game_picture']);
        unlink($photoPath);
        //$res=$usersmodel->where("id=$id")->delete();
        if ($gamesModel->delete($game_id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }


    public function addgame(){
        $this->display();
    } 

    // 崔晓真 添加比赛
    Public function dogame(){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;

        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']); 
        //dump($file);      
        $GameModel = M('games'); // 实例化User模型
        $GameModel->game_name =  I('post.game_name'); // 设置用户的创建时间
        $GameModel->uploaddate =  date("Y-m-d H:i:s"); // 设置用户的创建时间
        $GameModel->game_address = I('post.game_address');
         //dump($data);
        if ($file['game_picture']['size']==0) {//没有上传轮播图片
            //$this->error('请上传背景图片！');                         
        }else{//有图片
            $info=$this->MyUpload();
            $GameModel->game_picture = $info['game_picture']['savename']; // 保存上传的照片根据需要自行组装
        }
        $addresult=$GameModel->add(); // 写入用户数据到数据库
        if (false !== $addresult){
            $this->success('上传比赛成功！', '../Game/gamelist');
        }else{ 
            $this->error('数据错误'); 
        } 
    }

    //崔晓真 上传头像
    public function MyUpload(){ 
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        //$upload->allowExts  = array('mp4','jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  =  './';
        $upload->savePath =  'Public/upload/gamepic/';// 设置附件上传目录

        $upload->autoSub = false;
        $upload->saveName = date("YmdHis").rand(100,999);//定义名字
        // 上传单个文件 
        $info = $upload->upload();                    
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            return $info;
        }
    } 

    // 崔晓真 修改比赛
    function editgame($game_id){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;
        
        $games = M('games');
        $res = $games->where("game_id = $game_id")->find($data);
        $this->list=$res;
        $this->display();
    }

    // 崔晓真 修改图片
    function doUpdate($game_id){
        $gamesmodel=M('games');
        if($_FILES['game_picture']['error'] == 0){
            $logoinfo = $this->MyUpload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
        $data['game_name']=I('post.game_name');
        $data['game_picture'] = $pname;
        $data['game_address']=I('post.game_address');
        $res=$gamesmodel->where("game_id = $game_id")->save($data);
        if (false!==$res){
            $this->success('信息修改成功','gamelist');
        } else{
            $this->error('信息修改失败');
        }
    }
}